package com.itcast.jdbc;

import java.sql.*;

/**
 * 执行 DDL 语句
 */
public class jdbcDemo6 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            // 1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 2.获取连接对象
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db3", "root", "root");
            // 3.定义 sql
            String sql = "SELECT * FROM account";
            // 4.获取执行对象
            stmt = connection.createStatement();
            // 5.执行 sql
            rs = stmt.executeQuery(sql);
            // 6.处理结果
            // 6.1让游标向下移动一行
            rs.next();
            // 6.2获取数据
            int id = rs.getInt(1);
            String name = rs.getString("name");
            double balance = rs.getDouble(3);
            System.out.println(id + "---" + name + "---" + balance);

            // 6.1让游标向下移动一行
            rs.next();
            // 6.2获取数据
            int id2 = rs.getInt(1);
            String name2 = rs.getString("name");
            double balance2 = rs.getDouble(3);
            System.out.println(id2 + "---" + name2 + "---" + balance2);

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {

            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
